<?php

class ProposersController extends AppController {

    var $name = 'Proposers';

    function add() {
        $this->set('est', $this->Session->read('estado'));
        $this->pageTitle = 'Adicionar Proponente';
        $this->layout = "ajax";
        $cod = $this->Session->read('cod');

        if ($proyecto = $this->Proposer->Proyect->find("first", array("conditions" => array("Proyect.codigo" => $cod)))) {
            $id = $proyecto['Proyect']['id'];
        } else {
            $this->Session->setFlash('No ha seleccionado Proyecto');
            $this->redirect(array('controller' => 'Proyects', 'action' => 'index'));
        }

        $this->set('id', $id);
        $this->set('departaments', $this->Proposer->City->Departament->find('list', array('order' => 'name ASC')));
        // $this->set('proposerTypes',$this->Proposer->ProposerType->find('list',array('order' => 'name ASC')));

        if (!empty($this->data)) {
            if ($this->Proposer->save($this->data)) {

                $this->Session->setFlash('Proponente adicionado  con exito');
                $this->redirect(array('controller' => 'Proposers', 'action' => 'index'));
            } else {
                $this->set('cities', $this->Proposer->City->find('list', array('conditions' => array('City.departament_id' => $this->data['Proposer']['departament_id']), 'order' => 'name ASC')));
                $this->Session->setFlash('error guardando datos');
            }
        }
    }

    function index() {
        $id = $this->Session->read('proy_id');
        if ($id == "") {
            $this->Session->setFlash('No ha seleccionado Proyecto');
            $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
        } else {
            $this->Proposer->recursive = -1;
            //$this->set( "proposers" ,$this->Proposer->findAllByProyectId($id));
            $this->set("proposers", $this->Proposer->find("all", array("conditions" => array("Proposer.proyect_id" => $id))));
            $this->set('cod', $this->Session->read('cod'));
        }
    }

    function select() {

        $this->set('cities', $this->Proposer->City->find('list', array(
                    'order' => 'name ASC',
                    'conditions' => array('City.departament_id' => $this->data['Proposer']['departament_id'])
                        )
                ));
    }

    function edit($id = null) {

        $this->set('est', $this->Session->read('estado'));
        $this->layout = "ajax";
        if (empty($this->data)) {
            $this->Proposer->recursive = -1;
            //$this->data = $this->Proposer->findById($id);
            $this->data = $this->Proposer->find("first", array("conditions" => array("Proposer.id" => $id)));
            $dept = $this->Proposer->City->field('departament_id', array('City.id' => $this->data['Proposer']['city_id']));
            $this->set('cities', $this->Proposer->City->find('list', array('order' => 'name ASC', 'conditions' => array('City.departament_id' => $dept))));
            $this->set('departaments', $this->Proposer->City->Departament->find('list', array('order' => 'name ASC')));
            $this->data['Proposer']['departament_id'] = $dept;
        } else {
            if ($this->Proposer->save($this->data)) {

                $this->redirect(array('controller' => 'Proposers', 'action' => 'index'));
                $this->Session->setFlash("Registro Editado Exitosamente");
            } else {
                $this->Session->setFlash("Error Guardando Datos");
                $this->set('departaments', $this->Proposer->City->Departament->find('list', array('order' => 'name ASC')));
                $this->set('cities', $this->Proposer->City->find('list', array('conditions' => array('City.departament_id' => $this->data['Proposer']['departament_id']), 'order' => 'name ASC')));
            }
        }
    }

    function delete($id) {
        if ($this->Proposer->delete($id)) {
            $this->Session->SetFlash('Registro Borrado exitosamente');
            $this->Redirect(array('controller' => 'Proposers', 'action' => 'index'));
        }
    }

}

?>